<template>
  <div>
     <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card class="box-card">
        
        <!-- 商品搜索区域 -->
        <el-row :gutter="20">
            <el-col :span="8">
                <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
                    <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
                </el-input>
            </el-col>
            <el-col :span="4">
                <el-button type="primary" @click="goAddPage">添加商品</el-button>
            </el-col>
        </el-row>
            <!-- 表格区域 -->
            <el-table
                :data="goodsList"
                style="width: 100%"
                border
                stripe
                >
                <el-table-column type="index" label="#"></el-table-column>
                <el-table-column
                    prop="goods_name"
                    label="商品名称"
                    >
                </el-table-column>
                <el-table-column
                    prop="goods_price"
                    label="商品价格(元)"
                    width="95">
                </el-table-column>
                <el-table-column
                    prop="goods_weight"
                    label="商品重量"
                    width="70">
                </el-table-column>
                <el-table-column
                    prop="add_time"
                    label="创建时间"
                    width="140">
                    <template v-slot="scope">
                        {{scope.row.add_time | dataFormat}}
                    </template>
                </el-table-column>
                
                <el-table-column label="操作" width="130px">
                    <template v-slot="scope">
                        <!-- 修改按钮 -->
                        <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.goods_id)"></el-button>
                        <!-- 删除按钮 -->
                        <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteGoods(scope.row.goods_id)"></el-button>
                        

                    </template>
                </el-table-column>
            </el-table>
             <!-- 页码区域 -->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
    </el-card>
     <!-- 修改分类区域 -->
            <el-dialog
        title="修改商品信息"
        :visible.sync="editDialogVisible"
        width="50%"
        @close="editDialogClosed"
        >
        <el-form :model="editForm" ref="editFormRef" label-width="100px">
            <el-form-item label="商品名称">
                <el-input v-model="editForm.goods_name"></el-input>
            </el-form-item>
            <el-form-item label="商品价格">
                <el-input v-model="editForm.goods_price"></el-input>
            </el-form-item>
            <el-form-item label="商品重量">
                <el-input v-model="editForm.goods_weight"></el-input>
            </el-form-item>
            
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="editDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="editGoods">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
name:'List',
data() {
    return {
         // 获取用户列表的参数
        queryInfo:{
            query:'',
            pagenum:1,
            pagesize:10
        },
        addGoodsFormVisible:false,
        total:0,
        goodsList:[],
        editDialogVisible:false,
        editForm:{}
    }
},
created() {
    this.getGoodsList()
},
methods: {
    async getGoodsList() {
        const {data:res}=await this.$http.get('goods',{params:this.queryInfo})
        // console.log(res);
        if(res.meta.status!==200) return this.$message.error(res.meta.msg)
        this.goodsList=res.data.goods
        this.total=res.data.total
    },
    // 删除用户
    async deleteGoods(id) {
        const comfirmResult=await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err=>err)
        // 确认删除confirm
        // 取消删除concel
        // console.log(comfirmResult);
        if(comfirmResult!=='confirm') {
            return this.$message.info('取消了删除')
        }
        const {data:res}=await this.$http.delete('goods/'+id)
        if(res.meta.status!==200) return this.$message.error('删除用户信息失败')
        this.$message.success('删除用户信息成功')
        this.getGoodsList()
    },
    // 修改用户信息
    async showEditDialog(id) {
        this.editDialogVisible=true
        // console.log(id);
        const {data:res}=await this.$http.get(`goods/${id}`)
        // console.log(res);
        if(res.meta.status!==200) return this.$message.error('查询商品信息失败')
        this.editForm=res.data
    },
    // pagesize的改变
    handleSizeChange(val) {
        // console.log(`每页 ${val} 条`);
        this.queryInfo.pagesize=val
        this.getGoodsList()
      },
    //   当前页码值
      handleCurrentChange(val) {
        // console.log(`当前页: ${val}`);
        this.queryInfo.pagenum=val
        this.getGoodsList()

      },
      goAddPage() {
        this.$router.push('/goods/add')
      },
      editDialogClosed() {
        this.$refs.editFormRef.resetFields()
    },
      editGoods() {
        this.$refs.editFormRef.validate(async valid=>{
        if(!valid) return
        const {data:res}=await this.$http.put(`goods/${this.goodsId}`,{
            goods_name:this.editForm.goods_name,
            goods_price:this.editForm.goods_price,
            goods_weight:this.editForm.goods_weight,
            goods_number:this.editForm.goods_number,
            goods_cat:"0"
        })
        // console.log(res);
        if(res.meta.status!==200) return this.$message.error('修改商品信息失败')
        this.$message.success('修改商品信息成功')
        this.editDialogVisible = false
        // console.log(res);
        this.getGoodsList()
        })
    },
},
computed:{
    goodsId() {
        return this.editForm.goods_id
    }
}
}
</script>

<style scoped>

</style>